<?php

namespace app\api\service;

use app\api\model\ProductRuleModel;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;

class ProductRuleService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 添加、编辑产品投保规则
     */
    public function changeProductRule($params)
    {
        $Validate = new Validate([
            'base_rule' => 'require',
            'insure_rule' => 'require',
            'time_rule' => 'require',
            'pro_id' => 'require',
        ], [
            'base_rule' => '缺少产品投保基础规则',
            'insure_rule' => '缺少产品投保费率规则',
            'time_rule' => '缺少产品投保日期规则',
            'pro_id' => '缺少产品id',
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $Validate = new Validate([
            'insure_num' => 'require',
            'insure_age' => 'require',
            'is_ele_sign' => 'require',
            'work_type' => 'require',
            'work_cate_insure' => 'require',
        ], [
            'insure_num' => '投保人数参数必填',
            'insure_age' => '承保年龄参数必填',
            'is_ele_sign' => '电子签章设置必填',
            'work_type' => '职业类别规则必填',
            'work_cate_insure' => '多类别是否共保必填'
        ]);
        if (!$Validate->check($params['base_rule']))
        {
            $this->error($Validate->getError());
        }
        $Validate = new Validate([
            'month' => 'require',
            'month_rate' => 'require',
            'correct_rate' => 'require',
            'is_month_rate' => 'require',
            'is_insure' => 'require',
            'is_add_person' => 'require',
            'is_change_person' => 'require',
        ], [
            'month' => '投保月份必填',
            'month_rate' => '投保系数必填',
            'correct_rate' => '批改系数必填',
            'is_month_rate' => '批改费率计算方式必填',
            'is_insure' => '是否可投保必填',
            'is_add_person' => '批改是否可增加人员必填',
            'is_change_person' => '批改是否可替换人员必填',
        ]);
        if (!$Validate->check($params['insure_rule']))
        {
            $this->error($Validate->getError());
        }

        $checkData = [
            'is_insure_option' => 'require',
            'is_insure_cut_off' => 'require',
            'is_insure_temporary_cut_off' => 'require',
            'insure_week_is_work' => 'require',
            'is_insure_temporary_overtime' => 'require',
            'is_insure_temporary_off_time' => 'require',
            'is_correction_option' => 'require',
            'is_correction_cut_off' => 'require',
            'is_correction_temporary_cut_off' => 'require',
            'correction_week_is_work' => 'require',
            'is_correction_temporary_overtime' => 'require',
            'is_correction_temporary_off_time' => 'require',
        ];
        $checkDataMsg = [
            'is_insure_option' => '投保时间是否可选必填',
            'is_insure_cut_off' => '投保是否设置截单时间必填',
            'is_insure_temporary_cut_off' => '投保是否设置临时截单时间必填',
            'insure_week_is_work' => '投保是否执行周六日为工作日必填',
            'is_insure_temporary_overtime' => '投保是否执行临时加班时间必填',
            'is_insure_temporary_off_time' => '投保是否执行临时休班时间必填',
            'is_correction_option' => '批改时间是否可选必填',
            'is_correction_cut_off' => '批改是否设置截单时间必填',
            'is_correction_temporary_cut_off' => '批改是否设置临时截单时间必填',
            'correction_week_is_work' => '批改是否执行周六日为工作日必填',
            'is_correction_temporary_overtime' => '批改是否执行临时加班时间必填',
            'is_correction_temporary_off_time' => '批改是否执行临时休班时间必填',
        ];
        if (!empty($params['time_rule']['is_insure_option']))
        {
            $checkData['insure_advance_days'] = 'require';
            $checkDataMsg['insure_advance_days'] = '投保提前购买天数必填';
        }
        if (!empty($params['time_rule']['is_correction_option']))
        {
            $checkData['correction_advance_days'] = 'require';
            $checkDataMsg['correction_advance_days'] = '批改提前购买天数必填';
        }
        if (!empty($params['time_rule']['is_insure_cut_off']))
        {
            $checkData['insure_cut_off_time'] = 'require';
            $checkDataMsg['insure_cut_off_time'] = '投保截单时间必填';
        }
        /*if (!empty($params['time_rule']['is_insure_temporary_cut_off']))
        {
            $checkData['insure_temporary_cut_off_time'] = 'require';
            $checkDataMsg['insure_temporary_cut_off_time'] = '投保临时截单时间必填';
        }*/
        if (!empty($params['time_rule']['is_insure_temporary_overtime']))
        {
            $checkData['insure_temporary_overtime'] = 'require';
            $checkDataMsg['insure_temporary_overtime'] = '投保临时加班时间必填';
        }
        if (!empty($params['time_rule']['is_insure_temporary_off_time']))
        {
            $checkData['insure_temporary_off_time'] = 'require';
            $checkDataMsg['insure_temporary_off_time'] = '投保临时休班时间必填';
        }
        if (!empty($params['time_rule']['is_correction_cut_off']))
        {
            $checkData['correction_cut_off_time'] = 'require';
            $checkDataMsg['correction_cut_off_time'] = '批改截单时间必填';
        }
        /*if (!empty($params['time_rule']['is_correction_temporary_cut_off']))
        {
            $checkData['correction_temporary_cut_off_time'] = 'require';
            $checkDataMsg['correction_temporary_cut_off_time'] = '批改临时截单时间必填';
        }*/
        if (!empty($params['time_rule']['is_correction_temporary_overtime']))
        {
            $checkData['correction_temporary_overtime'] = 'require';
            $checkDataMsg['correction_temporary_overtime'] = '批改临时加班时间必填';
        }
        if (!empty($params['time_rule']['is_correction_temporary_off_time']))
        {
            $checkData['correction_temporary_off_time'] = 'require';
            $checkDataMsg['correction_temporary_off_time'] = '批改临时休班时间必填';
        }
        $Validate = new Validate($checkData, $checkDataMsg);
        if (!$Validate->check($params['time_rule']))
        {
            $this->error($Validate->getError());
        }
        Db::startTrans();
        try{
            $saveData = [
                'pro_id' => $params['pro_id'],
                'base_rule' => json_encode($params['base_rule']),
                'insure_rule' => json_encode($params['insure_rule']),
                'time_rule' => json_encode($params['time_rule'])
            ];

            foreach ($params['insure_rule']['month'] as $k => $v)
            {
                $error = false;
                if (!isset($params['insure_rule']['month_rate']))
                {
                    $error = true;
                }
                if (!isset($params['insure_rule']['correct_rate']))
                {
                    $error = true;
                }
                if (!isset($params['insure_rule']['is_insure']))
                {
                    $error = true;
                }
                if (!isset($params['insure_rule']['is_add_person']))
                {
                    $error = true;
                }
                if (!isset($params['insure_rule']['is_change_person']))
                {
                    $error = true;
                }
                if ($error)
                {
                    $this->error('投保费率方案配置错误');
                }
            }

            LogService::getInstance()->writeSysLog([
                'operation' => '/api/product_rule/changeProductRule',
                'title' => '设置投保规则',
                'content' => $params,
            ]);

//            if (!empty($params['pro_id']))
//            {
//                $saveData['u_time'] = datetime(time(), 'Y-m-d H:i:s');
//                $res = ProductRuleModel::getInstance()->update($params['pro_id'], $saveData);
//            }else{
                ProductRuleModel::getInstance()->delete($params['pro_id']);
                $saveData['c_time'] = datetime(time(), 'Y-m-d H:i:s');
                $res = ProductRuleModel::getInstance()->insert($saveData);
//            }
            Db::commit();
            return $res;
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取产品投保规则
     */
    public function getProductRule($params)
    {
        $Validate = new Validate([
            'pro_id' => 'require'
        ], [
            'pro_id' => '产品id必填'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $res = ProductRuleModel::getInstance()->find($params['pro_id']);
        if (!empty($res))
        {
            $res['base_rule'] = json_decode($res['base_rule'], true);
            $res['insure_rule'] = json_decode($res['insure_rule'], true);
            $res['time_rule'] = json_decode($res['time_rule'], true);
        }

        return $res ?: '';
    }
}